﻿#include <iostream>
#include <string>
#include "DynObj.h"
using namespace std;

class Api
{
public:
    virtual void test(string s) = 0;
protected:
    Api(){}// 屏蔽构造函数，实现接口隔离
};
class Impl : public Api{
public:
    void test(string s){
        cout << "现在是impl类， 正在执行功能方法" << s << endl;
    }
};

class ImplOne : public  Api{
public:
    void test(string s){
        cout << "现在是ImplOne类， 正在执行功能方法" << s << endl;
    }
};

class ImplTwo : public  Api{
public:
    void test(string s){
        cout << "现在是ImplTwo类， 正在执行功能方法" << s << endl;
    }
};

class Factory
{
public:
    static Api* createApi(int type){
        Api *pApi = nullptr;
        switch (type) {
        case 1:
            pApi = new ImplOne();
            break;
        case 2:
            pApi = new ImplTwo();
            break;
        default:
            break;
        }
        return pApi;
    }
    static Api* createApi(){
        return new ImplOne;
    };
};

class ImplTwoHelper{
public:

    ImplTwoHelper(){
        CObjectFactory::registerClass("ImplTwo", ImplTwoHelper::createObjFunc);
    }

    static void* createObjFunc(){
        return new ImplTwo;
    }
};


int main(int argc, char *argv[])
{
    Api *pApi = (ImplTwo*)CObjectFactory::createObject("ImplTwo");
    pApi->test("动态创建");
   // pApi->test("使用工厂方法重构");
    system("pause");
    return 0;
}
